package view;

import java.awt.Color;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.Font;

import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.ListCellRenderer;
import javax.swing.ListModel;
import javax.swing.UIManager;
import javax.swing.table.DefaultTableModel;

import net.miginfocom.swing.MigLayout;
import model.Computation;

public class OutputTable extends JPanel {
    JScrollPane popScrollPane;
    JScrollPane sampleScrollPane;
    JScrollPane sampleMeanScrollPane;
    JLabel popLabel;
    JTable popTable;
    JLabel sampleLabel;
    JTable sampleTable;
    JLabel sampleMeanLabel;
    JTable sampleMeanTable;
    DefaultTableModel popModel;
    DefaultTableModel sampleModel;
    DefaultTableModel sampleMeanModel;
    JList popRowHeaders;
    JList sampleRowHeaders;
    
    String[] columnNames = new String[] {};
    String[] RowData = new String[] {"x", "f(x)"};
    String[] sampleMeanColumnNames = new String[] {"Sample", "Mean"};
    
    public OutputTable() {
    	this.setLayout(new MigLayout());
    	
        initComponents();
        addComponents();
    }
    private class RowHeaderRenderer extends JLabel implements ListCellRenderer {
    	 
        RowHeaderRenderer(JTable table) {
 
            setOpaque(true);
            setBorder(UIManager.getBorder("TableHeader.cellBorder"));
            setHorizontalAlignment(CENTER);
            setForeground(new Color(50,50,50));
            setFont(new Font("Arial", Font.BOLD, 10));
    	}
 
        public Component getListCellRendererComponent( JList list, Object value, int index, boolean isSelected, boolean cellHasFocus) {
 
            setText((value == null) ? "" : value.toString());
            return this;
        }
    }
    private void initComponents() {
        popModel = new DefaultTableModel(columnNames, 0) {
            @Override
            public boolean isCellEditable(int row, int column) {
                return false;
            }
        };
        
        sampleModel = new DefaultTableModel(columnNames, 0) {
            @Override
            public boolean isCellEditable(int row, int column) {
                return false;
            }
        };
        
        sampleMeanModel = new DefaultTableModel(sampleMeanColumnNames, 0) {
            @Override
            public boolean isCellEditable(int row, int column) {
                return false;
            }
        };
        
        popLabel = new JLabel("Population Distribution Table");
        popTable = new JTable(popModel);
        popTable.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
        popTable.setFocusable(false);
        popTable.setRowSelectionAllowed(false);
        popTable.setEnabled(false);
        
        sampleLabel = new JLabel("Sample Distribution Table");
        sampleTable = new JTable(sampleModel);
        sampleTable.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
        sampleTable.setFocusable(false);
        sampleTable.setRowSelectionAllowed(false);
        sampleTable.setEnabled(false);
        
        sampleMeanLabel = new JLabel("Sample Mean Table");
        sampleMeanTable = new JTable(sampleMeanModel);
        sampleMeanTable.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
        sampleMeanTable.setFocusable(false);
        sampleMeanTable.setRowSelectionAllowed(false);
        sampleMeanTable.setEnabled(false);
        
        popScrollPane = new JScrollPane(popTable);
        popScrollPane.setPreferredSize(new Dimension(400, 150));
        sampleScrollPane = new JScrollPane(sampleTable);
        sampleScrollPane.setPreferredSize(new Dimension(400, 150));
        sampleMeanScrollPane = new JScrollPane(sampleMeanTable);
        sampleMeanScrollPane.setPreferredSize(new Dimension(400, 300));
        
        popRowHeaders = new JList(RowData);
        popRowHeaders.setFixedCellWidth(50);
        popRowHeaders.setCellRenderer(new RowHeaderRenderer(popTable));
        
        sampleRowHeaders = new JList(RowData);
        sampleRowHeaders.setFixedCellWidth(50);
        sampleRowHeaders.setCellRenderer(new RowHeaderRenderer(sampleTable));
 
        popScrollPane.setRowHeaderView(popRowHeaders);
        sampleScrollPane.setRowHeaderView(sampleRowHeaders);
    }
    
    private void addComponents() {
    	add(popLabel, "wrap");
        add(popScrollPane, "wrap 20");
        add(sampleLabel, "wrap");
        add(sampleScrollPane, "wrap");
        add(sampleMeanLabel, "wrap");
        add(sampleMeanScrollPane, "wrap");
    }
    
    
    public void updateData(Computation computation) {
    	    	
    	//population
    	
    	 popModel = new DefaultTableModel(columnNames, 0) {
             @Override
             public boolean isCellEditable(int row, int column) {
                 return false;
             }
         };
   
        Object[] popData = new Object[2];
        
        for (int i = 0; i < computation.getPopulation().size(); i++) {
            popData[0] = computation.getPopulation().get(i);
            popData[1] = computation.getPopulationMeans().get(i);
            popModel.addColumn(i, popData);
        }
        
        popTable.setModel(popModel);
        
        //sample
        
        sampleModel = new DefaultTableModel(columnNames, 0) {
            @Override
            public boolean isCellEditable(int row, int column) {
                return false;
            }
        };

        Object[] sampleData = new Object[2];
        
        
        for (int i = 0; i < computation.getUniqueMeanList().size(); i++) {
            sampleData[0] = computation.getUniqueMeanList().get(i);
            sampleData[1] = computation.getfunctionX().get(i);
            sampleModel.addColumn(i, sampleData);
        }
        
        sampleTable.setModel(sampleModel);
        
        //sampleMean
        
        sampleMeanModel = new DefaultTableModel(sampleMeanColumnNames, 0) {
            @Override
            public boolean isCellEditable(int row, int column) {
                return false;
            }
        };
        
        Object[] sampleMeanData = new Object[2];
        
        
        for (int i = 0; i < computation.getSamples().size(); i++) {
            sampleMeanData[0] = computation.getSamples().get(i);
            sampleMeanData[1] = computation.getSampleMeans().get(i);
            sampleMeanModel.addRow(sampleMeanData);
        }
        
        sampleMeanTable.setModel(sampleMeanModel);
    }
}
